import Vue from 'vue'
import Router from 'vue-router'
import ContactList from '@/components/ContactList'
import DepartmenttList from '@/components/DepartmentList'
import Login from '@/components/Login'
import EmployeeDetails from '@/components/EmployeeDetails'

Vue.use(Router)

const router =  new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'contact-list',
      component: ContactList,
      meta: {needAuth: true}
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {needAuth: false}
    },
    {
      path: '/departments',
      name: 'department-list',
      component: DepartmenttList,
      meta: {needAuth: true}
    },
    {
      path: '/employees/:id',
      name: 'employee-details',
      component: EmployeeDetails,
      meta: {needAuth: true}
    },
  ]
})

router.beforeEach(function(to,from,next){
  console.log(to)
  let user = JSON.parse(localStorage.getItem('user'))
  if(to.meta.needAuth && !user ){
    next('/login')
  }
  else{
    next()
  }
})

export default router